// pages/games/guess/guess.js

// 页面后台变量
var score=0 // 得分
var current=1 // 当前题号
var app=getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {

    current: 1,
    total: 2,
    type:0,
    i:0,
    dataList:[],
    uid:0
  },
getXzqb:function(type)
{
  var that=this
  wx.request({
    url: app.data.service_url+'game/getXzqbList', //改成你服务端的方法
    data: {
      type:type
    },
    method:"POST",
    header:{
      "Content-Type": "application/x-www-form-urlencoded"
       },  
    success: function(res) {
      console.log(res)

      that.setData({
        dataList:res.data
      })


    }
  })


},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    score=0 // 得分归0
    var that=this
    var type=options.game_type
    that.setData({
      type:type
    })
    that.getXzqb(type)
  },
  judgeAnswer(event){
    console.log(event)
    wx.showLoading({
      title: '正在提交',
    })
    var that=this
    let value=event.currentTarget.dataset.value
    let id=event.currentTarget.dataset.id
     console.log(value)
   that.setData({
    current:parseInt(that.data.current+1)
   })

   var jieguo=[{id:id,xuanxiang:value}]
   that.addXzqb(jieguo)


  },
  //提交雪中求抱
  addXzqb:function(jieguo)
  {

      var that=this
      wx.request({
        url: app.data.service_url+'game/addJieguo_xzqb', //改成你服务端的方法
        data: {
          uid:that.data.uid,
          jieguo:JSON.stringify(jieguo),
          game_type:that.data.type,
          type:3 //雪中求抱
        },
        method:"POST",
        header:{
          "Content-Type": "application/x-www-form-urlencoded"
           },  
        success: function(res) {
          wx.hideLoading({})
          console.log(res)
          var code=res.data['code']
          if(code==1)
          {
          var jieguo_id=res.data['jieguo_id']
          wx.showToast({
            title: '挑战结束',
            icon:"none",
            success(res)
            {
              console.log(res)
          
              setTimeout(function(){
                  wx.navigateTo({
                url: '/pages/games/guess/res?jieguo_id='+jieguo_id,
                 })
                },800)
            
            }
          })
    
          }else{
            wx.showToast({
              title: '网络错误',
              icon:"none",
              success()
              {
                wx.switchTab({
                  url: '/pages/tabs/home/home',
                })
              }
            })
          }
    
    
        }
      })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  var that=this
  wx.getStorage({
    key:"uid",
    success(res)
    {
      that.setData({
        uid:res.data
      })
    }
  })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})